﻿#region Copyright
// Copyright (c) 2011-2012 RaisingForce Team (Alikin Sergey)
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System;
using System.Globalization;
using System.Windows.Controls;

namespace RaisingForce.Raise.Presentation.ViewModel.Specialized
{
    public class SliderSettingViewModel :
        SettingViewModel
    {
        #region Constructors

        public SliderSettingViewModel(
            Action<double> setRealProperty,
            Func<double> getSettingValue = null)
            : base()
        {
            this.setRealProperty = setRealProperty;
            this.getSettingValue = getSettingValue;
            if (getSettingValue != null)
            {
                this.value = getSettingValue();
            }
            if (setRealProperty != null)
            {
                setRealProperty(this.value);
            }
        }

        #endregion

        #region Private Declarations

        private readonly Func<double> getSettingValue;

        private void updateValue()
        {
            if (this.getSettingValue != null)
            {
                this.Value = this.getSettingValue();
            }
        }

        private readonly DoubleValueFormatConverter converter =
            new DoubleValueFormatConverter();

        private readonly Action<double> setRealProperty;

        private string getValueText(double value)
        {
            return (string)this.converter
                .Convert(value, typeof(Slider), null, CultureInfo.CurrentCulture);
        }

        #endregion

        #region Base Overrides

        protected override void invalidateValue()
        {
            base.invalidateValue();
            this.updateValue();
        }

        #endregion

        #region ViewModel Properties

        private double value = 0.5d;

        public double Value
        {
            get { return this.value; }
            set
            {
                if (this.value != value)
                {
                    this.value = value;
                    if (this.setRealProperty != null)
                    {
                        this.setRealProperty(value);
                    }
                    this.OnPropertyChanged("Value");
                    this.OnPropertyChanged("ValueText");
                }
            }
        }

        public string ValueText
        {
            get { return this.getValueText(this.value); }
        }

        private int labelMinWidth;

        public int LabelMinWidth
        {
            get { return this.labelMinWidth; }
            set
            {
                this.SetProperty(
                    ref this.labelMinWidth,
                    value,
                    "LabelMinWidth");
            }
        }

        private double minimum = 0.0d;

        public double Minimum
        {
            get { return this.minimum; }
            set
            {
                this.SetProperty(
                    ref this.minimum,
                    value,
                    "Minimum");
            }
        }

        private double maximum = 1.0d;

        public double Maximum
        {
            get { return this.maximum; }
            set
            {
                this.SetProperty(
                    ref this.maximum,
                    value,
                    "Maximum");
            }
        }

        private double valueStep = 0.1d;

        public double ValueStep
        {
            get { return this.valueStep; }
            set
            {
                this.SetProperty(
                    ref this.valueStep,
                    value,
                    "ValueStep");
            }
        }

        #endregion

        #region Converter Properties

        public string ValueFormat
        {
            get { return this.converter.FormatString; }
            set
            {
                if (this.converter.FormatString != value)
                {
                    this.converter.FormatString = value;
                    this.OnPropertyChanged("ValueText");
                }
            }
        }

        public double ValueMultiplier
        {
            get { return this.converter.Multiplier; }
            set
            {
                if (this.converter.Multiplier != value)
                {
                    this.converter.Multiplier = value;
                    this.OnPropertyChanged("ValueText");
                }
            }
        }

        #endregion
    }
}
